package com.gupao.vip.controller;

import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.gupao.vip.MyProperty;

/**
 * Created by 17082655 on 2019/1/5.
 * 首页地址：  http://localhost:8081/index/index.htm
 */
@RestController
public class IndexController {
	@Autowired
	private MyProperty property;
	@GetMapping("/index.htm")
	public String index(HttpServletRequest request){
		//获取项目的根路径  两者一样  /index
		System.out.println("contextPath="+request.getContextPath());
		System.out.println("realPath="+request.getServletContext().getContextPath());

		//获取"/"的实际地址
		System.out.println("realPath="+request.getServletContext().getRealPath("/"));
		return "hello springBoot";
	}
	@GetMapping("/student.htm")
	public void student(HttpServletRequest request, HttpServletResponse response)throws Exception{
		/*Student s  =new Student();
		s.setId(1L);
		s.setName("melody");
		return s;*/
		OutputStream outputStream = response.getOutputStream();
		Properties properties = new Properties();
		properties.setProperty("person.id","1");
		properties.setProperty("person.name","melody");
		//将此 Properties 表中的属性列表（键和元素对）写入输出流
		properties.store(new OutputStreamWriter(outputStream),"Written by web server");
		outputStream.write("hello".getBytes());
	}

	@GetMapping("/property.htm")
	public String property(HttpServletRequest request){
		return property.toString();
	}

	@GetMapping("/thymeleaf.htm")
	public ModelAndView thymeleaf() {
		ModelAndView view = new ModelAndView();
		// 设置跳转的视图 默认映射到 src/main/resources/templates/{viewName}.html
		view.setViewName("index");
		// 设置属性
		view.addObject("title", "11");
		view.addObject("desc", "欢迎进入Luis-web 系统");
		Author author = new Author();
		author.setAge(24);
		author.setEmail("12@qq.sscom");
		author.setName("melodddddddy");
		view.addObject("author", author);
		return view;
	}
	/*private final JdbcTemplate jdbcTemplate;

	public IndexController(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	@GetMapping("/student.htm")
	public void queryUsers() {
		// 查询所有用户
		String sql = "select * from student";
		List<Map<String, Object>> students =  jdbcTemplate.queryForList(sql);
		System.out.println(students);
	}*/
	

}
